
function init() {
    load_options();
    //init_google_login();
}

function load_options() {
    for (var key in options) {
        if (options[key].type == undefined) continue;

        read_option(key, function (key, value) {
            var option = options[key];
            var input = $('#' + key + ':input');

            if (option.type == 'bool') {
                if (value) {
                    input.attr('checked','checked');
                } else {
                    input.removeAttr("checked");
                }
            } else {
                input.val(value);
            }

            if (option.readonly) {
                input.attr('disabled','disabled');
            }

            if (key == 'douban_event_sync') {
                $('#douban_event_calendar').toggle(value);
            }
        });

        $('#' + key + ':input').change(function () {
            var key = $(this).attr('id');
            var option = options[key];
            var value = null;

            if (option.type == 'bool') {
                value = $(this).attr('checked') != undefined;
            } else {
                value = $(this).val();
            }

            write_option(key, value, function (key, value) {
                console.log("updated option " + key + " = " + value);
            });

            if (key == 'douban_event_sync') {
                $('#douban_event_calendar').toggle(value);
            }
        });
    }

    read_option('douban_event_last_sync_time', function (key, value) {
        $('#douban_event_last_sync_time').text(value || '从未同步');
    });

    if (read_option('douban_access_token') &&
        read_option('douban_access_token_secret')) {
        $('#douban_oauth_button').val("更新授权");
    }
}

function douban_oauth() {
    chrome.extension.sendRequest({
        action: 'oauth'
    }, function (response) {
        if (response.access_token) {
            $('#douban_access_token').val(response.access_token);
            $('#douban_oauth_button').val("更新授权");
        } else {
            $('#douban_access_token').val("");
            $('#douban_oauth_button').val("获取授权");
        }
    });
}

function douban_event_sync_all() {
    console.log("begin to sync douban events");

    $('#douban_event_last_sync_time').text('正在同步...');
    $('#syncing').show();

    chrome.extension.sendRequest({
        action: 'event_sync'
    }, function (response) {
        $('#syncing').hide();

        read_option('douban_event_last_sync_time', function (key, value) {
            $('#douban_event_last_sync_time').text(value || '从未同步');
        });
    });
}
/*
 var EVENT_FEED_URL = "https://www.google.com/calendar/feeds/";

 function init_google_login() {
 google.gdata.client.init(function (exc) {
 if (e instanceof Error) {
 // Alert with the error line number, file and message.
 console.log('Error at line ' + e.lineNumber + ' in ' +
 e.fileName + '\n' + 'Message: ' + e.message);

 // If available, output HTTP error code and status text
 if (e.cause) {
 var errorStatus = e.cause.status;
 var statusText = e.cause.statusText;

 console.log('Root cause: HTTP error ' + errorStatus + ' with status text of: ' + statusText);
 }
 } else {
 console.log(e.toString());
 }
 });

 var token = google.accounts.user.checkLogin(EVENT_FEED_URL);

 console.log("Google user token <" + token + ">");

 toggle_login_button(token);
 }

 function toggle_login_button(token) {
 if (token) {
 $('#google_login_token').val(token);
 $('#google_login_button').val('退出').unbind('click').click(function () {
 google.accounts.user.logout();
 });
 } else {
 $('#google_login_button').val('登录').unbind('click').click(function () {
 google.accounts.user.login(EVENT_FEED_URL);
 });
 }
 }
 */

function show_douban_event_map() {
    var api = DOUBAN.create_api();

    api.getCurrentUser(function (user) {
        var url = chrome.extension.getURL('/pages/eventsmap/eventsmap.html') + '?' + $.param({
            uid: user.uid,
            action: 'participate'
        });

        chrome.extension.sendRequest({
            action: 'open',
            url: url
        }, function (response) {
        });
    });
}

function run_unit_tests() {
    chrome.extension.sendRequest({
        action: 'unittests',
    }, function (response) {
    });
}

$(function () {
    init();
});